/*
 * Copyright 2010 Gal Dolber.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.guit.demo.client.jsonserializer;

import com.google.gwt.editor.client.SimpleBeanEditorDriver;
import com.google.gwt.resources.client.CssResource;
import com.google.gwt.user.client.ui.HasText;
import com.google.inject.Inject;

import com.guit.client.GuitPresenter;
import com.guit.client.apt.GwtPresenter;
import com.guit.client.binder.GuitBinder;
import com.guit.client.binder.GwtEditor;
import com.guit.client.binder.ViewField;
import com.guit.client.binder.ViewHandler;
import com.guit.client.binder.contributor.RunAsync;
import com.guit.client.binder.contributor.UserAction;
import com.guit.client.binder.useraction.Category;
import com.guit.client.binder.useraction.HasUserActionValue;
import com.guit.client.jsorm.JsonSerializer;
import com.guit.client.place.Place;
import com.guit.client.place.PlaceDataEncrypted;
import com.guit.demo.client.jsonserializer.JsonSerializerPresenter.JsonSerializerBinder;
import com.guit.demo.client.main.MainPresenter;

import java.util.Date;

@RunAsync
@Category("Json serializer")
@GwtPresenter(placeName = "json")
@PlaceDataEncrypted
@GwtEditor(pojo = Person.class, paths = {"nombre=name"})
public class JsonSerializerPresenter extends GuitPresenter<JsonSerializerBinder> implements
    Place<Person>, HasUserActionValue {

  public static interface CustomStyle extends CssResource {
    String style1();

    String style2();
  }

  public interface JsonSerializerBinder extends GuitBinder<JsonSerializerPresenter> {
  }

  public static interface PersonSerializer extends JsonSerializer<Person> {
  }

  // Count actions for user action tracking
  private int actionsCount = 0;

  @ViewField
  CustomStyle style;

  @ViewField
  HasText json;

  private final PersonSerializer personSerializer;
  private final MainPresenter hasContent;

  Person data;

  SimpleBeanEditorDriver<Person, ?> driver;

  @Inject
  public JsonSerializerPresenter(MainPresenter hasContent, PersonSerializer personSerializer) {
    this.hasContent = hasContent;
    this.personSerializer = personSerializer;
  }

  @Override
  public int getUserActionNumber() {
    return actionsCount;
  }

  @Override
  public String getUserActionText() {
    return "something";
  }

  @Override
  public void go(Person data) {
    hasContent.setContent(getView(), 5);
    if (data == null) {
      data = new Person();
      data.setAddress("Agate rd 418");
      data.setBirthday(new Date());
      data.setId(13l);
      data.setName("Gal Dolber");
    }
    setPerson(data);
    json.setText(personSerializer.serialize(data));
    this.data = data;
  }

  @ViewHandler
  @UserAction("EditForm")
  void id$nombre$address$birthday$valueChange() {
    data = driver.flush();
    placeManager.newItem(JsonSerializerPresenter.class, data);
    json.setText(personSerializer.serialize(data));

    // The user action tracker gets triggered after the handler, so it will
    // never track 0
    actionsCount++;
  }

  @ViewHandler
  @UserAction("Json edited")
  void json$valueChange() {
    data = personSerializer.deserialize(json.getText());
    placeManager.newItem(JsonSerializerPresenter.class, data);
    setPerson(data);

    // The user action tracker gets triggered after the handler, so it will
    // never track 0
    actionsCount++;
  }

  private void setPerson(Person person) {
    driver.edit(person);
  }
}
